<template>
  <el-dialog v-dialog-drag append-to-body :close-on-click-modal="false" width="600px" :visible.sync="visible"
    :before-close="beforeClose" :title="title">
    <div class="wrapper">
      <div>注:【排查类别、时间、角色、频次、允许生成任务】</div>
      <div style="margin-top: 5px; margin-bottom: 20px">
        全部设置后可以生成任务
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="排查类别" prop="excludeType">
          <el-checkbox-group v-model="form.excludeType">
            <el-checkbox v-for="item in dict.type.biz_exclude_type" :key="item.value" :label="item.value">
              {{ item.label }}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="排查频次" prop="dateType">
          <el-input-number style="width: 120px" v-model="form.excludeFrequency" placeholder="请输入排查频次" :min="0" />次/
          <el-select style="width: calc(100% - 144px)" v-model="form.dateType" placeholder="请选择时间类型(日,周,月,季度,半年)">
            <el-option v-for="(item, index) in dateTypeOptions" :key="index" :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="userIds">
          <el-select v-model="form.userIds" placeholder="请选择角色">
            <el-option v-for="(item, index) in roleIdOptions" :key="index" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排查时间" prop="dateRange">
          <el-date-picker v-model="form.dateRange" type="daterange" range-separator="-" value-format="yyyy-MM-dd"
            start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" size="mini" @click="submit" v-if="operateType != 'view'">确定</el-button>
      <el-button size="mini" @click="beforeClose">取消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getRoles, configData } from "../api";
export default {
  name: "ConfigDialog",
  dicts: ["biz_exclude_type"],
  props: {
    visible: Boolean,
    title: {
      type: String,
      default: "质量检查计划详情"
    },
    data: {
      type: Object,
      default: () => { }
    },
    operateType: {
      type: String,
      default: "view"
    },
    dateTypeOptions: {
      type: Array,
      default: () => []
    },
  },
  data() {
    return {
      projectId: this.$store.state.app.selectData,
      roleIdOptions: [],
      form: {
        excludeType: [],
        excludeFrequency: null,
        dateType: null,
        dateRange: [],
        userIds: null,
        id: ""
      },
      // 表单校验
      rules: {
        excludeType: [
          { required: true, message: "排查类别不能为空", trigger: "change" },
        ],
        dateType: [
          { required: true, message: "频次类不能为空", trigger: "change" },
        ],
        dateRange: [
          { required: true, message: "频次类不能为空", trigger: "change" },
        ],
        userIds: [
          { required: true, message: "频次类不能为空", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.initData();
  },
  methods: {
    initData() {
      this.form.id = this.data.id
      getRoles().then((data) => {
        const res = data.roles.map(item => ({ label: item.roleName, value: item.roleId }))
        this.roleIdOptions = res;
      });
      if (this.operateType == 'view') {
        Object.keys(this.rules).forEach(item => {
          delete this.rules[item]
        })
      }
      if (this.operateType == 'config') {
        const aaa = this.data;
        Object.keys(this.data).forEach(key => {
          if (key == 'excludeType') {
            this.data[key] && this.$set(this.form, key, this.data[key].split(","))
          }else if(key == 'userIds'){
            this.data[key].length && this.$set(this.form, key, this.data[key][0])
          } else {
            this.$set(this.form, key, this.data[key])
          }
        })
      }
    },
    beforeClose() {
      this.$emit('update:visible', false);
    },
    async submit() {
      this.$refs.form.validate(async valid => {
        if (!valid) {
          return;
        }
        const res = await configData({ ...this.form, userIds: [this.form.userIds], excludeType: this.form.excludeType.join(","), projectId: this.projectId })
        if (res.code == 200) {
          this.$emit("refresh")
          this.beforeClose()
        }
      })
    },
  },
};
</script>
<style lang="scss" scoped>
.wrapper {
  max-height: 500px;
  overflow-y: auto;
  overflow-x: hidden;

  .el-select {
    width: 100%;
  }

  .el-date-editor {
    width: 100%;
  }
}
</style>
